package com.yeyks.controller.activity;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.account.utils.AccountUtil;
import com.yeyks.activity.dal.domain.ActivityList;
import com.yeyks.activity.dal.domain.ActivityPrizeInfo;
import com.yeyks.activity.param.*;
import com.yeyks.activity.dto.ActivityListDTO;
import com.yeyks.activity.service.ActivityListService;
import com.yeyks.activity.service.ActivityPrizeInfoService;
import com.yeyks.annotation.NeedToken;
import com.yeyks.annotation.SystemLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.constants.SysLogMsg;
import com.yeyks.common.constants.activity.ActivityPrizeInfoTypeConstant;
import com.yeyks.common.param.PageParam;
import com.yeyks.commonReference.service.activity.ActivityWebBaseService;
import com.yeyks.commonReference.service.other.CouponInfoService;
import com.yeyks.other.dal.domain.CouponInfo;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * v3.8双旦活动模块
 *
 * @author shuai
 * @since 2019-11-28
 */
@Api(tags = "v3.8双旦活动")
@Slf4j
@RestController
@RequestMapping("/activity/double/dan")
public class ActivityDoubleDanController {

    @Autowired
    private ActivityWebBaseService activityWebBaseService;
    @Autowired
    private ActivityListService activityListService;
    @Autowired
    private ActivityPrizeInfoService activityPrizeInfoService;
    @Autowired
    private CouponInfoService couponInfoService;

    @ApiOperation(value = "v3.8 --> 活动列表", response = ActivityList.class)
    @PostMapping(value = "/list")
    public AjaxResponseBody<PageResult<ActivityList>> list(PageParam page) {
        PageResult<ActivityList> pageResult = activityWebBaseService.getActivityList(page);
        return AjaxResponseBody.success(pageResult);
    }

    @ApiOperation(value = "v3.8 --> 活动详情", response = ActivityListDTO.class)
    @PostMapping(value = "/detail")
    public AjaxResponseBody<ActivityListDTO> detail(Integer id) {
        ActivityListDTO activityListDTO = activityWebBaseService.detail(id);
        return AjaxResponseBody.success(activityListDTO);
    }

    @SystemLog(SysLogMsg.ACTIVITY_UPDATE)
    @NeedToken
    @ApiOperation(value = "v3.8 --> 活动修改")
    @PostMapping(value = "/update")
    public AjaxResponseBody<Boolean> update(@Validated ActivityListUpdateParam param) {
        ActivityList byId = activityListService.getById(param.getId());
        Arguments.beanIsNull(byId, "该活动不存在");
        if (null != param.getEndTime()) {
            byId.setEndTime(new Date(param.getEndTime()));
        }
        if (StrUtil.isNotBlank(param.getName())) {
            byId.setName(param.getName());
        }
        if (StrUtil.isNotBlank(param.getNoteApp())) {
            byId.setNoteApp(param.getNoteApp());
        }
        if (StrUtil.isNotBlank(param.getNoteH5())) {
            byId.setNoteH5(param.getNoteH5());
        }
        if (StrUtil.isNotBlank(param.getRemark())) {
            byId.setRemark(param.getRemark());
        }
        byId.setUpdateTime(new Date());
        return AjaxResponseBody.success(activityListService.updateById(byId));
    }

    @SystemLog(SysLogMsg.ONE_PRIZE_ADD)
    @NeedToken
    @ApiOperation(value = "v3.8 --> 添加一级奖品")
    @PostMapping(value = "/one/prize/add")
    public AjaxResponseBody<Integer> onePrizeAdd(@Validated ActivityPrizeOneAddParam param) {
        param.setOperatorId(AccountUtil.getOperationUserId(AccountUtils.getLoginUser()));
        return AjaxResponseBody.success(activityWebBaseService.onePrizeAdd(param));
    }

    @SystemLog(SysLogMsg.ONE_PRIZE_UPDATE)
    @NeedToken
    @ApiOperation(value = "v3.8 --> 修改一级奖品")
    @PostMapping(value = "/one/prize/update")
    public AjaxResponseBody<Boolean> onePrizeUpdate(@Validated ActivityPrizeOneUpdateParam param) {
        param.setOperatorId(AccountUtil.getOperationUserId(AccountUtils.getLoginUser()));
        Boolean flag = activityWebBaseService.onePrizeUpdate(param);
        return AjaxResponseBody.success(flag);
    }

    @SystemLog(SysLogMsg.ONE_PRIZE_DEL)
    @NeedToken
    @ApiOperation(value = "v3.8 --> 删除一级奖品")
    @PostMapping(value = "/one/prize/del")
    public AjaxResponseBody<Boolean> onePrizeDel(Integer prizeId) {
        activityWebBaseService.onePrizeDel(prizeId);
        return AjaxResponseBody.success(true);
    }

    @SystemLog(SysLogMsg.TWO_PRIZE_ADD)
    @NeedToken
    @ApiOperation(value = "v3.8 --> 添加二级奖品")
    @PostMapping(value = "/two/prize/add")
    public AjaxResponseBody<Boolean> twoPrizeAdd(@Validated ActivityPrizeTwoAddParam param) {
        param.setOperatorId(AccountUtil.getOperationUserId(AccountUtils.getLoginUser()));
        return AjaxResponseBody.success(activityWebBaseService.twoPrizeAdd(param));
    }

    @SystemLog(SysLogMsg.TWO_PRIZE_UPDATE)
    @NeedToken
    @ApiOperation(value = "v3.8 --> 修改二级奖品")
    @PostMapping(value = "/two/prize/update")
    public AjaxResponseBody<Boolean> twoPrizeUpdate(@Validated ActivityPrizeTwoUpdateParam param) {
        param.setOperatorId(AccountUtil.getOperationUserId(AccountUtils.getLoginUser()));
        Boolean flag = activityWebBaseService.twoPrizeUpdate(param);
        return AjaxResponseBody.success(flag);
    }

    @SystemLog(SysLogMsg.TWO_PRIZE_DEL)
    @NeedToken
    @ApiOperation(value = "v3.8 --> 删除二级奖品")
    @PostMapping(value = "/two/prize/del")
    public AjaxResponseBody<Boolean> twoPrizeDel(Integer prizeId) {
        activityWebBaseService.twoPrizeDel(prizeId);
        return AjaxResponseBody.success(true);
    }

    @ApiOperation(value = "v3.8 --> 通过一级奖品id查询二级奖品", response = ActivityPrizeInfo.class)
    @PostMapping(value = "/two/prize/list")
    public AjaxResponseBody<List<ActivityPrizeInfo>> twoPrizeList(Integer prizeId) {
        Arguments.intIsNull(prizeId, "一级奖品id不能为空");
        List<ActivityPrizeInfo> list = activityPrizeInfoService.list(new QueryWrapper<ActivityPrizeInfo>().eq(ActivityPrizeInfo.PARENT_ID, prizeId));
        for (ActivityPrizeInfo activityPrizeInfo : list) {
            if (ActivityPrizeInfoTypeConstant.COUPON.equals(activityPrizeInfo.getPrizeType())) {
                CouponInfo byId = couponInfoService.getById(activityPrizeInfo.getCouponId());
                if (ObjectUtil.isNotNull(byId)) {
                    activityPrizeInfo.setPubCount(byId.getCount());
                    activityPrizeInfo.setTakeCount(byId.getGetCount());
                    activityPrizeInfo.setRemainCount(byId.getRemainCount());
                }
            }
        }
        return AjaxResponseBody.success(list);
    }

    @ApiOperation(value = "v3.8 --> 双旦券列表", response = CouponInfo.class)
    @PostMapping(value = "/coupon/list")
    public AjaxResponseBody<List<CouponInfo>> couponList() {
        List<CouponInfo> list = activityWebBaseService.couponList(10);
        return AjaxResponseBody.success(list);
    }
}
